package com.lxr0860.lxr0860production_test.utils
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.lxr0860.lxr0860production_test.bean.CountInfo


object ShareUtils {
    private var sps:SharedPreferences?=null
    private fun getSps(context: Context): SharedPreferences {
        if(sps==null){
            sps=context.getSharedPreferences("key_switch_count",Context.MODE_PRIVATE)
        }
        return sps!!
    }
    /**
     *
     * 保存计数
     */
    fun putCountInfo(countInfo: CountInfo, context:Context){
        val editor:SharedPreferences.Editor=getSps(context).edit()
        editor.clear()
        val toJson = Gson().toJson(countInfo)
        editor.putString("countInfo",toJson)
        editor.apply()
    }

    /**
     * 读取计数
     */
    fun getCountInfo(context: Context): CountInfo? {
            val sps:SharedPreferences=getSps(context)
        val string = sps.getString("countInfo", "")
        return Gson().fromJson(string,CountInfo::class.java)
    }

    /**
     * 清空测试数据
     */
    @SuppressLint("CommitPrefEdits")
    fun clearTestCount(context: Context){
        val editor:SharedPreferences.Editor=getSps(context).edit()
        editor.clear()
        editor.apply()
    }
}